package com.travel.server.manager.impl.command.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.data.QueryWholeDataResponse;
import com.travel.server.manager.dto.response.data.QueryWholeDataResponse.TrendItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.model.dto.data.WholeAllDto;
import com.travel.server.manager.model.dto.data.WholeDataDto;
import com.travel.server.manager.model.dto.data.WholeTrendDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 17:13
 * @Desc:
 */
@Component
@Slf4j
public class QueryWholeDataCommand extends AbstractCommand<EmptyRequest, QueryWholeDataResponse> {

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public QueryWholeDataResponse handle(EmptyRequest request) {
        Date now = new Date();
        Date yesterday = DateUtil.getIndexDay(now, Calendar.DAY_OF_MONTH, -1);
        Date yesterdayStartTime = DateUtil.setDayStart(yesterday);
        Date yesterdayEndTime = DateUtil.setDayEnd(yesterday);
        Date todayEndTime = DateUtil.setDayEnd(now);

        WholeAllDto totalDto = CollectionUtil.getFirst(orderDal.selectAllData(null, null));
        WholeAllDto yesterdayDto = CollectionUtil.getFirst(orderDal.selectAllData(yesterdayStartTime, yesterdayEndTime));
        Integer yesterdayOrder = yesterdayDto.getTotalAmount();
        Integer totalOrder = totalDto.getTotalAmount();

        List<WholeDataDto> wholeDataDtoList = orderDal.selectWholeData();
        WholeDataDto dto = CollectionUtil.getFirst(wholeDataDtoList);
        QueryWholeDataResponse response = new QueryWholeDataResponse();
        response.setYesterdayTrade(CommonUtil.fenToYuan(yesterdayOrder).toString());
        response.setTotalTrade(CommonUtil.fenToYuan(totalOrder).toString());
        response.setTotalOrder(dto.getTotal() + "");
        response.setTotalApplyOrder(dto.getApply() + "");
        response.setTotalProjectOrder(dto.getProject() + "");
        response.setTotalRefundOrder(dto.getRefund() + "");
        response.setApplyOrder(CommonUtil.getProportion(dto.getProject(), dto.getApply()));
        response.setRefundRatio(CommonUtil.getProportion(dto.getRefund(), dto.getTotal()));
        response.setWaitExam(dto.getWaitExam() + "");
        response.setWaitPay(dto.getWaitPay() + "");
        response.setWaitContract(dto.getWaitContract() + "");
        response.setWaitData(dto.getWaitData() + "");
        response.setProjectIng(dto.getProjectIng() + "");
        response.setProjectEnd(dto.getProjectEnd() + "");
        response.setProjectRefund(dto.getOrderRefund() + "");
        response.setVisit("0");
        response.setYesterdayVisit("0");
        response.setBrowser("0");
        response.setYesterdayBrowser("0");
        response.setYesterdayPayNum(yesterdayDto.getPayNum() + "");
        response.setPayNum(totalDto.getPayNum() + "");
        response.setPayOrder(response.getPayNum());
        response.setYesterdayPayOrder(response.getYesterdayPayNum());

        response.setTrendList(makeTrendList(yesterdayStartTime, todayEndTime));
        return response;
    }

    public List<TrendItem> makeTrendList(Date startTime, Date endTime){
        List<TrendItem> trendList = new ArrayList<>();
        List<WholeTrendDto> list = orderDal.selectWholeTrend(startTime, endTime, CommonConstant.YES);
        Map<Integer, Integer> todayMap = makeMap(list, true);
        Map<Integer, Integer> yesterdayMap = makeMap(list, false);
        for(int i = 0; i < 24; i++){
            TrendItem item = new TrendItem();
            item.setTime(i + "");
            item.setToday(getTotal(todayMap, i));
            item.setYesterday(getTotal(yesterdayMap, i));
            trendList.add(item);
        }
        return trendList;
    }

    private String getTotal(Map<Integer, Integer> data, Integer time){
        Integer total = 0;
        for(int i = 0; i <= time; i++){
            Integer timeData = data.get(i);
            timeData = timeData == null ? 0 : timeData;
            total += timeData;
        }
        return CommonUtil.fenToYuan(total).toString();
    }

    private Map<Integer, Integer> makeMap(List<WholeTrendDto> list, boolean isToday){
        Map<Integer, Integer> map = new HashMap<>();
        if(!CollectionUtil.isEmpty(list)){
            Date now = new Date();
            for(WholeTrendDto dto : list) {
                Date date = DateUtil.strToDate(dto.getDate(), DateUtil.YYYY_MM_DD);
                if(isToday){
                    if(DateUtil.isSameDay(now, date)){
                        map.put(dto.getTime(), dto.getAmount());
                    }
                }else{
                    if(!DateUtil.isSameDay(now, date)){
                        map.put(dto.getTime(), dto.getAmount());
                    }
                }
            }
        }
        return map;
    }
}
